package com.hongpu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hongpu.config.SecurityContextUtil;
import com.hongpu.domain.SysMenu;
import com.hongpu.domain.SysRoleMenu;
import com.hongpu.mapper.SysRoleMenuMapper;
import com.hongpu.service.SysRoleMenuService;
import com.hongpu.utils.MenuWithCheckedTreeUtils;
import com.hongpu.utils.ResultDto;
import com.hongpu.utils.RoleMenuTreeUtils;
import com.hongpu.vo.MenuTreeVo;
import com.hongpu.vo.SysMenuVo;
import com.hongpu.vo.SysRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Michael zhang
* @description 针对表【sys_role_menu(角色-菜单关联关系表)】的数据库操作Service实现
* @createDate 2025-06-06 10:39:03
*/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
    implements SysRoleMenuService {

    @Autowired
    SysRoleMenuMapper baseMapper;

    /**
     * 根据角色ID和菜单ID保存角色-菜单关系
     *
     * @param roleId
     * @param menuId
     */
    @Override
    public boolean saveRoleIdAndMenuId(Long roleId, Long menuId) {
        try {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId).setMenuId(menuId);
            boolean add = baseMapper.add(sysRoleMenu);
            return add;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 新增
     *
     * @param sysRoleMenu
     */
    @Override
    public ResultDto add(SysRoleMenu sysRoleMenu) {
        try {
            boolean add = baseMapper.add(sysRoleMenu);
            return add ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public ResultDto delete(Long id) {
       try {
           boolean delete = baseMapper.delete(id, SecurityContextUtil.getUserId());
           return delete ? ResultDto.ok() : ResultDto.fail();
       } catch (Exception e) {
           return ResultDto.fail(e.getLocalizedMessage());
       }
    }

    /**
     * 根据角色ID删除角色菜单关系
     *
     * @param roleId
     */
    @Override
    public boolean removeByRoleId(Long roleId) {
        return baseMapper.removeByRoleId(roleId);
    }

    /**
     * 启用
     *
     * @param id
     */
    @Override
    public ResultDto active(Long id) {
        try {
            boolean active = baseMapper.active(id, SecurityContextUtil.getUserId());
            return active ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 修改
     *
     * @param sysRoleMenu
     */
    @Override
    public ResultDto update(SysRoleMenu sysRoleMenu) {
        try  {
            boolean update = baseMapper.update(sysRoleMenu);
            return update ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * @param roleId
     */
    @Override
    public ResultDto queryMenuIdByRoldId(Long roleId) {
        try  {
            List<SysMenu> menuList = baseMapper.queryMenuIdByRoldId(roleId);
            return ResultDto.ok(menuList);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 查询
     *
     * @param roleId
     */
    @Override
    public ResultDto queryMenuTreeByRoldId(Long roleId) {
        try  {
            List<SysMenu> menuList = baseMapper.queryMenuIdByRoldId(roleId);
            RoleMenuTreeUtils roleMenuTreeUtils = new RoleMenuTreeUtils(menuList);
            List<SysMenuVo> tree = roleMenuTreeUtils.buildTree();
            return ResultDto.ok(tree);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 根据角色ID集合查询角色-菜单关系树列表
     *
     * @param roleIds
     */
    @Override
    public List<SysMenuVo> queryMenuTreeByRoldIds(List<SysRoleVo> roleIds) {
        try {
            List<SysMenu> menuList = baseMapper.queryMenuByRoldIds(roleIds);
            RoleMenuTreeUtils roleMenuTreeUtils = new RoleMenuTreeUtils(menuList);
            List<SysMenuVo> tree = roleMenuTreeUtils.buildTree();
            return tree;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 编辑角色时用到的菜单下拉列表树
     *
     * @param roleId
     */
    @Override
    public ResultDto menuTreeWithCheckedList(Long roleId) {
        try  {
            List<MenuTreeVo> menuList = baseMapper.queryMenuWithCheckedByRoldId(roleId);

            MenuWithCheckedTreeUtils menuWithCheckedTreeUtils = new MenuWithCheckedTreeUtils(menuList);
            List<MenuTreeVo> tree = menuWithCheckedTreeUtils.buildTree();
            return ResultDto.ok(tree);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }
}




